home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / gs3.53 / viewgif.ps < prev    next >
Text File  |  1996-01-10  |  5KB  |  163 lines

  1. %    Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of Aladdin Ghostscript.
  3. % Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  4. % or distributor accepts any responsibility for the consequences of using it,
  5. % or for whether it serves any particular purpose or works at all, unless he
  6. % or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  7. % License (the "License") for full details.
  8. % Every copy of Aladdin Ghostscript must include a copy of the License,
  9. % normally in a plain ASCII text file named PUBLIC.  The License grants you
  10. % the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. % under certain conditions described in the License.  Among other things, the
  12. % License requires that the copyright notice and this notice be preserved on
  13. % all copies.
  14.  
  15. % viewgif.ps
  16. % Display a GIF file.
  17.  
  18. /read1            % <file> read1 <int>
  19.  { read pop
  20.  } bind def
  21. /read2            % <file> read2 <int>
  22.  { dup read1 exch read1 8 bitshift add
  23.  } bind def
  24.  
  25. /readGIFheader        % <file> readGIFheader <dict>
  26.  { 20 dict begin
  27.    dup 6 string readstring pop
  28.    dup (GIF87a) eq exch (GIF89a) eq or not
  29.     { (Not a GIF file.\n) print cleartomark stop
  30.     } if
  31.    dup read2 /Width exch def
  32.    dup read2 /Height exch def
  33.    dup read1
  34.    dup 128 ge /GlobalColor exch def
  35.    dup -4 bitshift 7 and 1 add /BitsPerPixel exch def    %***BOGUS?***
  36.    dup 8 and 0 ne /PaletteSorted exch def
  37.    7 and 1 add dup /BitsPerPixel exch def
  38.      1 exch bitshift /PaletteSize exch def
  39.    dup read1 /BackgroundIndex exch def
  40.    dup read1 15 add 64 div /AspectRatio exch def
  41.    GlobalColor
  42.     { PaletteSize 3 mul string readstring pop
  43.       /GlobalPalette exch def
  44.     } if
  45.    currentdict end
  46.  } bind def
  47.  
  48. /readGIFimageHeader    % <file> readGIFimageHeader <dict>
  49.             % Note: GIF header must be on dict stack
  50.  { 10 dict begin
  51.     { dup read1
  52.       dup (!) 0 get ne { exit } if pop        % extension
  53.       dup read1 pop
  54.        { dup read1 dup 0 eq { pop exit } if { dup read1 pop } repeat
  55.        } loop
  56.     } loop
  57.    (,) 0 get ne
  58.     { (Not a GIF image.\n) print stop
  59.     } if
  60.    dup read2 /Left exch def
  61.    dup read2 /Top exch def
  62.    dup read2 /Width exch def
  63.    dup read2 /Height exch def
  64.    dup read1
  65.    dup 128 ge /LocalColor exch def
  66.    dup 64 and 0 ne /Interlaced exch def
  67.    LocalColor
  68.     { 7 and 1 add /BitsPerPixel exch def
  69.       1 BitsPerPixel bitshift 3 mul string readstring pop
  70.       /Palette exch def
  71.     }
  72.     { pop pop /Palette GlobalPalette def
  73.     }
  74.    ifelse
  75.    currentdict end
  76.  } bind def
  77.  
  78. /imageGIF        % <imagedict> imageGIF
  79.  { /ImageOut where
  80.     { pop
  81.         % We know BitsPerComponent = 8, Decode = [0 255].
  82.         % and there is only a single data source which is
  83.         % either a filter or a string whose size is exactly
  84.         % the width of the row.
  85.       dup /DataSource get dup type /stringtype eq
  86.        { ImageOut exch writestring
  87.        }
  88.        { pop dup /Width get string
  89.      1 index /Height get
  90.       { 1 index /DataSource get 1 index readstring pop
  91.         ImageOut exch writestring
  92.       }
  93.      repeat pop pop
  94.        }
  95.       ifelse 
  96.     }
  97.     { image
  98.     }
  99.    ifelse
  100.  } bind def
  101.  
  102. /viewGIF        % <file|string> viewGIF -
  103.  { save 20 dict begin
  104.    /saved exch def
  105.    dup type /stringtype eq { (r) file } if
  106.    /F exch def
  107.    /ImageOutFile where { /ImageOut ImageOutFile (w) file def } if
  108.    F readGIFheader /Header exch def
  109.      currentdict Header end begin begin
  110.    DEBUG { Header { exch == == } forall (----------------\n) print flush } if
  111.    F readGIFimageHeader /ImageHeader exch def
  112.      currentdict ImageHeader end begin begin
  113.    DEBUG { ImageHeader { exch == == } forall (----------------\n) print flush } if
  114.    /D F
  115.    <<    /InitialCodeLength F read1
  116.     /FirstBitLowOrder true
  117.     /BlockData true
  118.     /EarlyChange 0
  119.    >> /LZWDecode filter def
  120.  
  121.    [/Indexed /DeviceRGB 1 BitsPerPixel bitshift 1 sub Palette] setcolorspace
  122.    matrix currentmatrix
  123.    0 1 3 { 2 copy get dup 0 ne { dup abs div } if 3 copy put pop pop } for
  124.    setmatrix
  125.    <<    /ImageType 1
  126.     /ImageMatrix [1 0 0 -1 0 Height]
  127.     /BitsPerComponent 8
  128.     /Decode [0 255]
  129.    Interlaced
  130.     {    /Width Width   /Height 1
  131.     /row Width string def
  132.     /DataSource row
  133.       >> /I exch def
  134.       /inter        % <num> <denom> inter -
  135.        { /denom exch def   /num exch def
  136.          gsave
  137.      /lines Height denom 1 sub add num sub denom idiv def
  138.      0 num neg translate
  139.      /delta denom neg def
  140.      lines
  141.       { D row readstring pop pop
  142.         I imageGIF
  143.         0 delta translate
  144.       } repeat
  145.      grestore
  146.        }
  147.       bind def
  148.       0 8 inter
  149.       4 8 inter
  150.       2 4 inter
  151.       1 2 inter
  152.     }
  153.     {    /Width Width   /Height Height
  154.     /DataSource D
  155.       >> imageGIF
  156.     }
  157.    ifelse
  158.    saved end end end restore
  159.  } bind def   
  160.